<html lang="en">
<head>
    <title>员工管理</title>
    <#include "../common/header.ftl">
    <script type="text/javascript" src="/js/plugins/jquery-validation/message/messages_cn.js"></script>
    <script>
        $(function () {
            /*validate 表单校验插件*/
            $('#editForm').validate({
                rules:{
                    email:{
                        required:true,
                        email:true
                    },
                    name:{
                        required:true,
                        minlength:2
                    },
                    password:"required",
                    repassword:{
                        equealTo:"#password"
                    }

                },
                messages:{
                    name:{
                        required:"用户必填",
                        minlength:"用户名不能低于两个字符"
                    },
                    password:"用户必填",
                    repassword:"两次输入的密码不一致"
                }
            });
        });
        $(function () {
            var roleDiv;
            //超级管理点击事件
            $("#admin").click(function () {
                if (this.checked) {
                    //如果选中, 删除角色div框
                    roleDiv = $("#role").detach();
                } else {
                    //如果取消, 添加角色div框
                    //this仅仅是超级管理员的 input标签
                    $(this).closest("div").after(roleDiv);
                }
            })


            ///提交表单, 全选自己角色下拉框
            $("#btn_submit").click(function () {
                //全选自己角色下拉框
                $(".selfRoles option").prop("selected", true);
                //表单提交
                $("#editForm").submit();
            });


            //1: 页面加载完毕之后, 将自己的角色[id]全部获取出来, 放置到一个数组中
            //参数1:操作数组
            //参数2:回调方法, 目的是后去遍历option数组中每个元素的value值
            //结果: 自己角色id数组
            var ids = $.map($(".selfRoles option"), function (option) {
                return option.value;
            });
            //2:将系统的角色[id]也全部获取出来
            var allRoles = $(".allRoles option");

            //遍历数组
            //参数1:数组
            //参数2:回调方法
            $.each(allRoles, function (index, item) {
                //3:遍历系统角色[id]一个个去跟 第一步获取到数组对比
                ///判断元素是否在数组中
                //参数1:判断元素
                //参数2:数组
                //返回: 如果存在, 返回数组索引, 不存在返回-1
                if ($.inArray(item.value, ids) >= 0) {
                    //3.1: 如果存在, 删除当前比较系统角色
                    $(item).remove();
                } else {
                    //3.2: 如果不存在, 保留
                }
            });

            //解决超管角色下拉框隐藏问题
            if ($("#admin").prop("checked")) {
                roleDiv = $("#role").detach();
            }

        });

        //全部移动
        function moveAll(srcCls, targetCls) {
            $("." + srcCls + " option").appendTo($("." + targetCls));
        }

        //选中移动
        function moveSelected(srcCls, targetCls) {
            $("." + srcCls + " option:selected").appendTo($("." + targetCls));
        }
    </script>
</head>
<body>
<!--左侧菜单回显变量设置-->
<#assign currentMenu="employee">
<div class="container " style="margin-top: 20px">
    <#include "../common/top.ftl">
    <div class="row">
        <div class="col-sm-2">
            <#include "../common/menu.ftl">
        </div>
        <div class="col-sm-10">
            <div class="row">
                <div class="col-sm-12">
                    <h1 class="page-head-line">员工编辑</h1>
                </div>
            </div>
            <div class="row col-sm-10">
                <form class="form-horizontal" action="/employee/saveOrUpdate.do" method="post" id="editForm">
                    <input type="hidden" value="${(entity.id)!}" name="id">
                    <div class="form-group">
                        <label for="name" class="col-sm-2 control-label">用户名：</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" id="name" name="name" value="${(entity.name)!}"
                                   placeholder="请输入用户名">
                        </div>
                    </div>
                    
                <#if !entity ??>
                    <div class="form-group">
                        <label for="password" class="col-sm-2 control-label">密码：</label>
                        <div class="col-sm-6">
                            <input type="password" class="form-control" id="password" name="password"
                                   placeholder="请输入密码">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="repassword" class="col-sm-2 control-label">验证密码：</label>
                        <div class="col-sm-6">
                            <input type="password" class="form-control" id="repassword" placeholder="再输入一遍密码">
                        </div>
                    </div>
                </#if>

                    <div class="form-group">
                        <label for="email" class="col-sm-2 control-label">Email：</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" id="email" name="email" value="${(entity.email)!}"
                                   placeholder="请输入邮箱">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="age" class="col-sm-2 control-label">年龄：</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" id="age" name="age" value="${(entity.age)!}"
                                   placeholder="请输入年龄">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="dept" class="col-sm-2 control-label">部门：</label>
                        <div class="col-sm-6">
                            <select class="form-control" id="dept" name="dept.id">
                                <#list depts as d>
                                    <option value="${d.id}">${d.name}</option>
                                </#list>
                            </select>
                        </div>
                        <script>
                            //部门回显
                            $("#dept option[value='${(entity.dept.id)!}']").prop("selected", true);
                        </script>

                    </div>

                    <div class="form-group">
                        <label for="admin" class="col-sm-2 control-label">超级管理员：</label>
                        <label class="checkbox-inline" style="margin-left: 15px;">
                            <input type="checkbox" id="admin" name="admin">
                        </label>
                        <script>
                            <#if entity ?? && entity.admin>
                                    $("#admin").prop("checked", true);
                            </#if>
                        </script>
                    </div>
                    <div class="form-group" id="role">
                        <div>
                            <label for="role" class="control-label" style="margin-left: 60px">角色：</label>
                        </div>
                        <div class="row" style="margin-top: 10px">
                            <div class="col-sm-4 col-sm-offset-1">
                                <select multiple class="form-control allRoles" size="15">
                                    <#list roles as role>
                                        <option value="${role.id}">${role.name}</option>
                                    </#list>
                                </select>
                            </div>
                            <div class="col-sm-2" style="margin-top: 60px;" align="center">
                                <div>
                                    <a type="button" class="btn btn-info  " style="margin-top: 10px"
                                       onclick="moveSelected('allRoles', 'selfRoles')">&nbsp;&gt;&nbsp;</a>
                                    <br>
                                    <a type="button" class="btn btn-info " style="margin-top: 10px"
                                       onclick="moveSelected('selfRoles', 'allRoles')">&nbsp;&lt;&nbsp;</a>
                                    <br>
                                    <a type="button" class="btn btn-info " style="margin-top: 10px"
                                       onclick="moveAll('allRoles', 'selfRoles')">&gt;&gt;</a>
                                    <br>
                                    <a type="button" class="btn btn-info " style="margin-top: 10px"
                                       onclick="moveAll('selfRoles', 'allRoles')">&lt;&lt;</a>
                                </div>
                            </div>

                            <div class="col-sm-4">
                                <select multiple class="form-control selfRoles" size="15" name="ids">
                                        <#if entity ?? && entity.roles ??>
                                            <#list entity.roles as role>
                                                <option value="${role.id}">${role.name}</option>
                                            </#list>
                                        </#if>
                                </select>
                            </div>
                        </div>
                    </div>

                    <div class="form-group">
                        <div class="col-sm-offset-1 col-sm-6">
                            <button id="btn_submit" type="button" class="btn btn-default">保存</button>
                            <button type="reset" class="btn btn-default">重置</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
</body>
</html>